package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.common.WebMvcInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private WebMvcInterceptor webMvcInterceptor;


    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(webMvcInterceptor)//添加拦截器
                .addPathPatterns("/**") //拦截路径
                .excludePathPatterns("/user/login")//移动端登录
                .excludePathPatterns("/user/sendMsg")//移动端验证码
                .excludePathPatterns("/employee/login")//排除的路径
                .excludePathPatterns("/backend/**")//排除的路径
                .excludePathPatterns("/front/**");//排除的路径
    }

    //扩建MVC消息转换器
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1.创建一个消息转换器对象MappingJackson2HttpMessageConverter()
        MappingJackson2HttpMessageConverter mc = new MappingJackson2HttpMessageConverter();
        //TOOD最核心的操作,设置核心对象
        mc.setObjectMapper(new JacksonObjectMapper());
        //2.将消息转换器对象添加到集合中(注意:需要在集合的最前面,优先级高些)
        converters.add(0, mc);
    }



}
